﻿$(function () {
    //样式
    $("input[type='radio']").parent().addClass("radio_label");
    if (navigator.userAgent.indexOf("Firefox") > 0) { $(":radio").hide(); }
    $("input[type='radio']").click(function () {
        $("input[type='radio'][name='" + $(this).attr('name') + "']").parent().removeClass("checked")
        $(this).parent().addClass("checked");
    });
});
function SaveConfirm() {
    $.ligerDialog.confirm('即将覆盖之前的诊疗内容，确定继续？', function (yes) {
        if (yes) {
            Save();
        } else {
            $.ligerDialog.alert("用户放弃了操作");
        }
    });
}
function Save() {
    var checkpass = true;
    $(":input[type=text]").each(function () {
        if ($(this).val().indexOf(",") >= 0 || $(this).val().indexOf("|") >= 0) {
            checkpass = false;
        }
    });
    if (!checkpass) {
        $.ligerDialog.error("输入框包含了非法字符");
        return false;
    }
    var html = '';
    var hasdate;
    var results = '[  ';
    var results2 = '';
    $('[tag]').each(function (i) {// 遍历
        var tag = $(this).attr('tag');
        var num = i + 1;//id附加的序列号
        if (tag == '0') {
            var wid = '';
            var title = '';
            var dis = '';
            title = document.getElementById('txttitle' + num).value;
            wid = document.getElementById('txtwidth' + num).value;
            dis = document.getElementById('discript' + num).value;
            html += '<tr><td style="text-align:right; ">' + title + '：</td><td><input type="text" id="txt' + i + '" class="l-text" style="width:' + wid + 'px;" value=""><font style="">&nbsp;' + dis + '</font></td></tr>';
            results += '{ "ControlTitle":"' + title + '" , "ControlId":' + '"txttitle' + num + '", "ControlName":"' + tag + '" , "ControlDescribe":"' + dis + '" }';
            //text
            //0:ControlType,ControlDescribe,ControlName,ControlTitle
            results2 += tag + ',' + dis + ',' + 'txttitle' + num + ',' + title + '|';
        }
        if (tag == '1') {
            var title = ''; var options = '';
            title = document.getElementById('txttitle' + num).value;
            options = document.getElementById('txtOptions' + num).value;
            html += '<tr><td style="text-align:right; ">' + title + '：</td><td>';
            if (typeof (options) != "undefined" && options != '') {
                var myArray = new Array();
                myArray = options.split("#"); //字符分割 
                if (myArray.length > 0) {
                    html += '<select>';
                    var x;
                    for (x in myArray) {
                        html += '<option value="' + myArray[x] + '">' + myArray[x] + '</option>';
                    }
                    html += '</select>';
                }
            }
            html += '</td></tr>';
            results += '{ "leftValue' + i + '":"' + title + '" , "controlId' + i + '":' + '"txttitle' + num + '", "controlName' + i + '":"' + tag + '" , "rightValue' + i + '":"' + options + '" },';
            //droplist
            //1:ControlType,Options,ControlName,ControlTitle
            results2 += tag + ',' + options + ',' + 'droptitle' + num + ',' + title + '|';
        }
        if (tag == '2') {
            var title = ''; var options = '';
            title = document.getElementById('txttitle' + num).value;
            options = document.getElementById('txtOptions' + num).value;
            html += '<tr><td style="text-align:right; ">' + title + '：</td><td>';
            if (typeof (options) != "undefined" && options != '') {
                var myArray = new Array();
                myArray = options.split("#"); //字符分割 
                var x;
                for (x in myArray) {
                    html += '<input type="radio" name="radio' + i + '" value="' + myArray[x] + '">' + myArray[x] + '&nbsp;&nbsp;';
                }
            }
            html += '</td></tr>';
            results += '{ "leftValue' + i + '":"' + title + '" , "controlId' + i + '":' + '"txttitle' + num + '", "controlName' + i + '":"' + tag + '" , "rightValue' + i + '":"' + options + '" },';
            //radiolist
            //2:ControlType,Options,ControlName,ControlTitle
            results2 += tag + ',' + options + ',' + 'radiotitle' + num + ',' + title + '|';
        }
        if (tag == '3') {
            var title = ''; var options = '';
            title = document.getElementById('txttitle' + num).value;
            options = document.getElementById('txtOptions' + num).value;
            html += '<tr><td style="text-align:right; ">' + title + '：</td><td>';
            if (typeof (options) != "undefined" && options != '') {
                var myArray = new Array();
                myArray = options.split("#"); //字符分割 
                var x;
                for (x in myArray) {
                    html += '<input type="checkbox" class="l-checkbox" name="chx' + i + '" value="' + myArray[x] + '">' + myArray[x] + '&nbsp;&nbsp;';
                }
            }
            html += '</td></tr>';
            results += '{ "leftValue' + i + '":"' + title + '" , "controlId' + i + '":' + '"txttitle' + num + '", "controlName' + i + '":"' + tag + '" , "rightValue' + i + '":"' + options + '" },';
            //checkboxlist
            //3:ControlType,Options,ControlName,ControlTitle
            results2 += tag + ',' + options + ',' + 'chktitle' + num + ',' + title + '|';
        }
        if (tag == '4') {
            var title = ''; var date = '';
            title = document.getElementById('txttitle' + num).value;
            var obj = document.getElementsByName('timetype' + num);
            for (i = 0; i < obj.length; i++) {
                if (obj[i].checked) {
                    date = obj[i].value;
                }
            }
            html += '<tr><td style="text-align:right; ">' + title + '：</td><td>';
            if (typeof (date) != "undefined" && date != '') {
                hasdate = 1;
                switch (date) {
                    case '0'://控件格式为：1900-01-01 00:00:00
                        html += '<input type="text" id="dt" class="l-text" lay-key="1">';
                        break;
                    case '1'://1900-01-01
                        html += '<input type="text" id="dt" class="l-text" lay-key="1">';
                        break;
                    case '2'://1900-01
                        html += '<input type="text" id="dt" class="l-text" lay-key="1">';
                        break;
                    case '3'://1900
                        html += '<input type="text" id="dt" class="l-text" lay-key="1">';
                        break;
                    case '4'://23:00:00
                        html += '<input type="text" id="dt" class="l-text" lay-key="1">';
                        break;
                }
            }
            html += '</td></tr>';
            results += '{ "leftValue' + i + '":"' + title + '" , "controlId' + i + '":' + '"txttitle' + num + '", "controlName' + i + '":"' + tag + '" , "rightValue' + i + '":"' + date + '" },';
            //dateformat ControlType,ControlTitle,ControlName,DateType
            results2 += tag + ',' + title + ',' + 'txtdatetime' + num + ',' + date + '|';
        }
    });
    if (hasdate == 1) {
        html += '<script>$(function () {laydate.render({elem: "#dt",type: "datetime"});});<\/script>';
    }
    if (results.length > 0)
        results = results.substring(0, results.length - 1);
    results += ']';
    var url = "../PathwaySet/SaveContent";
    $.post(url, {
        rnd: new Date().getMilliseconds(),
        jsonStr: results,
        results2: results2,
        pathId: $("#hdnId").val()
    }, function (data, textStatus) {
        if (textStatus == "success") {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                $.ligerDialog.success("保存成功", function () {
                    parent.window.btnSearch.click();
                    frameElement.dialog.close();
                });
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        }
    })
}
var number = 1;//id附加的序列号
$(document).ready(function () {
    $("#add_new_line").click(function () {
        var type = $('input:radio[name="checktype"]:checked').val();
        if (type == "0") {
            var addHtml = $('#divAdd').html();
            var w = 'txtwidth' + number;
            addHtml = addHtml.replaceAll('txtwidth', w);
            var discript = 'discript' + number;
            addHtml = addHtml.replaceAll('discript', discript);
        }
        if (type == "1") {
            var addHtml = $('#divAddDropDown').html();
            var txtOptions = 'txtOptions' + number;
            addHtml = addHtml.replaceAll('txtOptions', txtOptions);
        }
        if (type == "2") {
            var addHtml = $('#divAddRadio').html();
            var txtOptions = 'txtOptions' + number;
            addHtml = addHtml.replaceAll('txtOptions', txtOptions);
        }
        if (type == "3") {
            var addHtml = $('#divAddCheck').html();
            var txtOptions = 'txtOptions' + number;
            addHtml = addHtml.replaceAll('txtOptions', txtOptions);
        }
        var timetypeId = "";
        if (type == "4") {
            var addHtml = $('#divAddDate').html();
            var timetype = 'timetype' + number;
            timetypeId = timetype;
            addHtml = addHtml.replaceAll('timetype', timetype);
        }
        var t = 'txttitle' + number;
        addHtml = addHtml.replaceAll('txttitle', t);
        $("#fileTable").append(addHtml);       
        if (timetypeId != "") {
            $("input[type='radio'][name=" + timetypeId + "]").click(function () {
                $("input[type='radio'][name='" + $(this).attr('name') + "']").parent().removeClass("checked");
                $(this).parent().addClass("checked");
            });
            timetypeId = "";
        }
        number = number + 1;
    });
    $("#delete_line").click(function () {
        if ((fileTable.rows.length - 1) > 1) {
            fileTable.deleteRow(fileTable.rows.length - 1);
            number = number - 1;
        }
    });
    loadContent();
});
String.prototype.replaceAll = function (s1, s2) {
    return this.replace(new RegExp(s1, "gm"), s2);
}
function loadContent() {
    $.ajax({
        url: "/ChestPainCenter/MedicalRecords/GetDiagnosticPathTemp?pathId=" + $("#hdnId").val() + "&patientId=",
        cache: false,
        success: function (data) {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                var jsonData = $.parseJSON(d.Body);
                for (var i = 0 ; i < jsonData.length; i++) {
                    var type = jsonData[i].ControlType;
                    $("input[type=radio][name=checktype][value=" + type + "]").attr("checked", 'checked');
                    $("input[type=radio][name=checktype]").parent().removeClass("checked");
                    $("input[type=radio][name=checktype][value=" + type + "]").parent().addClass("checked");
                    $("#add_new_line").click();
                    $("#txttitle" + (number - 1)).val(jsonData[i].ControlTitle);
                    switch (type) {
                        case 0:
                            $("#discript" + (number - 1)).val(jsonData[i].ControlDescribe);
                            break;
                        case 1:
                            $("#txtOptions" + (number - 1)).val(jsonData[i].Options);
                            break;
                        case 2:
                            $("#txtOptions" + (number - 1)).val(jsonData[i].Options);
                            break;
                        case 3:
                            $("#txtOptions" + (number - 1)).val(jsonData[i].Options);
                            break;
                        case 4:
                            var timeFormat = jsonData[i].TimeFormart.toString();
                            var timeFormatId = 0;
                            switch (timeFormat) {
                                case "yyyy-MM-dd HH:mm:ss":
                                    timeFormatId = 0;
                                    break;
                                case "yyyy-MM-dd":
                                    timeFormatId = 1;
                                    break;
                                case "yyyy-MM":
                                    timeFormatId = 2;
                                    break;
                                case "yyyy":
                                    timeFormatId = 3;
                                    break;
                                case "HH:mm:ss":
                                    timeFormatId = 4;
                                    break;
                                default:
                                    break;
                            }
                            $("input[type=radio][name=timetype" + (number - 1) + "][value=" + timeFormatId + "]").attr("checked", 'checked');
                            if (navigator.userAgent.indexOf("Firefox") > 0) { $(":radio").hide(); }
                            break;
                        default:
                            break;
                    }
                }
                
            }
        }
    });
}